package com.xiaotu.spider.maoyan.pageprocessor;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.MaoYanMovieBasicInfoModel;
import com.xiaotu.common.model.MaoYanMovieRecordOnlineModel;
import com.xiaotu.common.util.CacheHandler;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DateUtils;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.server.service.maoyan.MaoYanMovieBasicInfoService;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;

@Component("MaoYanMovieCheckOnlinePageProcessor")
public class MaoYanMovieCheckOnlinePageProcessor extends AUrlPageProcessor {

	private String [] LIST_URL = {
			"https://www.baidu.com/"
	};
	
	private String todayDate = DateUtils.getDateString(new Date(), DateUtils
            .FILE_NOMENCLATURE_FORMAT);
    
    private String yesterdayDate = DateUtils.getSpecifiedDayBefore(todayDate,DateUtils
            .FILE_NOMENCLATURE_FORMAT);
	
    private List<MaoYanMovieBasicInfoModel> yDayList = new ArrayList<MaoYanMovieBasicInfoModel>();
    
    private List<MaoYanMovieBasicInfoModel> nDayList = new ArrayList<MaoYanMovieBasicInfoModel>();
    
    private List<MaoYanMovieBasicInfoModel> newOnlineList = new ArrayList<MaoYanMovieBasicInfoModel>();
 
    @Autowired
    protected CacheHandler cacheHandler;
    
    @Autowired
    private MaoYanMovieBasicInfoService maoYanMovieBasicInfoService;
    
	@SuppressWarnings("unchecked")
	@Override
	public void process(Page page) {
		
		List<MaoYanMovieRecordOnlineModel> maoYanMovieRecordOnlineModelList = new ArrayList<MaoYanMovieRecordOnlineModel>();
		
		
		List<Map<String,Object>> todayDataList = (List<Map<String,Object>>)cacheHandler.hGet(todayDate+"_MAOYAN_DATALIST_"+DateUtils.fomartDateToString(new Date(), "yyyyMMdd"),
				"MAOYAN_DATALIST", List.class);
		List<Map<String,Object>> yesterdayDataList = (List<Map<String,Object>>)cacheHandler.hGet(yesterdayDate+"_MAOYAN_DATALIST_"+DateUtils.fomartDateToString(new Date(), "yyyyMMdd"),
				"MAOYAN_DATALIST", List.class);
		
		for(Map<String,Object> mov : todayDataList){
	    	MaoYanMovieBasicInfoModel basicInfo = new MaoYanMovieBasicInfoModel();
	    	String dataId = mov.get("movieId") == null?"":RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER, StringUtil
	                .nullToString(mov.get("movieId")));
	    	basicInfo.setDataid(dataId);
	    	nDayList.add(basicInfo);
        }
		for(Map<String,Object> mov : yesterdayDataList){
	    	MaoYanMovieBasicInfoModel basicInfo = new MaoYanMovieBasicInfoModel();
	    	String dataId = mov.get("movieId") == null?"":RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER, StringUtil
	                .nullToString(mov.get("movieId")));
	    	basicInfo.setDataid(dataId);
	    	yDayList.add(basicInfo);
        }
	    if(!yDayList.isEmpty() && yDayList.size()>0 && !nDayList.isEmpty() && nDayList.size()>0){
	    	for(MaoYanMovieBasicInfoModel yModel : yDayList){
	    		boolean temp = false;
	    		for(MaoYanMovieBasicInfoModel nModel : nDayList){
	    			if(yModel.getDataid().equals(nModel.getDataid())){
	    				temp = true;
	    			}
	    		}
	    		if(!temp){
	    			yModel.setStatus(1);//已下线
	    			yModel.setOfflineDate(yesterdayDate);//昨天下线
	    		}else{
	    			yModel.setStatus(0);//未下线
	    			yModel.setOfflineDate(null);//将之前的下线记录清除
	    		}
	    	}
	    	
	    	//确定新上线
	    	for(MaoYanMovieBasicInfoModel nModel : nDayList){
	    		boolean temp2 = false;
	    		for(MaoYanMovieBasicInfoModel yModel : yDayList){
	    			if(yModel.getDataid().equals(nModel.getDataid())){
	    				temp2 = true;
	    			}
	    		}
	    		if(!temp2){
	    			newOnlineList.add(nModel);
	    		}
    		}
	    	
	    	yDayList.addAll(nDayList);
	    	List<MaoYanMovieBasicInfoModel> resultList = maoYanMovieBasicInfoService.queryMovieBasicInfoList(yDayList);
	    	yDayList.removeAll(nDayList);
	    	for(int i =0;i<resultList.size();i++){
	    		MaoYanMovieBasicInfoModel tempModel = resultList.get(i);
	    		for(MaoYanMovieBasicInfoModel yModel : yDayList){
	    			if(tempModel.getDataid().equals(yModel.getDataid())){
	    				if(yModel.getStatus()==1){
	    					tempModel.setStatus(1);
	    					tempModel.setOfflineDate(yesterdayDate);//昨天下线
	    					
	    					MaoYanMovieRecordOnlineModel maoYanMovieRecordOnlineModel = new MaoYanMovieRecordOnlineModel(
	    							//tempModel.getId(),
	    							null,
	    							tempModel.getTvname(),
	    							tempModel.getDataid(),
	    							Constants.SiteName.MAOYAN,
	    							tempModel.getOnlineDate(),
	    							yesterdayDate,
	    							null
	    	    					);
	    					
	    					maoYanMovieRecordOnlineModelList.add(maoYanMovieRecordOnlineModel);
	    					
	    				}else if(yModel.getStatus()==0){
	    					tempModel.setStatus(0);
	    					//确定之前是否存在下线记录 
	    					if(null != tempModel.getOfflineDate() && todayDate.compareTo(tempModel.getOfflineDate())>0){
	    						tempModel.setOnlineDate(todayDate);
	    					}
	    					tempModel.setOfflineDate(null);//将之前的下线记录清除
	    				}
	    			}
	    		}
	    		for(MaoYanMovieBasicInfoModel nOnlieModel : newOnlineList){
	    			if(tempModel.getDataid().equals(nOnlieModel.getDataid())){
    					tempModel.setStatus(0);
    					tempModel.setOnlineDate(todayDate);
    					tempModel.setOfflineDate(null);//将之前的下线记录清除
	    			}
	    		}
	    	}
	    	
	    	maoYanMovieBasicInfoService.updateBatchMovieBasicInfo(resultList,maoYanMovieRecordOnlineModelList);
	    }
	    
	}

	@Override
	protected String[] getStartRequestUrls() {
		return new String[]{LIST_URL[0]};
	}

	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.MOVIE_BOXOFFICE;
	}

	@Override
	public String getPageDataType(Page page) {
        return Constants.PageDataType.SEARCH_LIST;
	}
	
}
